import { ArmatureController } from "./ArmatureController";
import { emitter } from "../../eventEmitter";
import { ModeType } from "./mode/ModeController";
import { Transform } from "../geom/Transform";

export class UIEventHandler {
    public ctrl: ArmatureController;
    constructor(ctrl: ArmatureController) {
        this.ctrl = ctrl;
        this.registerEvent();
    }

    registerEvent() {
        emitter.on("updateTransform", (e) => {
            const transform = new Transform();
            transform.x = e.x;
            transform.y = e.y;
            transform.scaleX = e.scaleX;
            transform.scaleY = e.scaleY;
            transform.rotation = e.rotation * Transform.DEG_RAD;

            this.ctrl.applyTransform2Item(transform);
        });

        emitter.on("changeModeType", (e) => {
            console.log("change mode ", e)
            if (e == "select") {
                this.ctrl.switchMode(ModeType.Transform);
            } else if (e == "createBone") {
                this.ctrl.switchMode(ModeType.BoneCreater);
            }
        });

        emitter.on("changeTransformModeType", (e) => {
            if (e == "transform") {
                this.ctrl.switchMode(ModeType.Transform);
            } else if (e == "translate") {
                this.ctrl.switchMode(ModeType.Translate);
            } else if (e == "scale") {
                this.ctrl.switchMode(ModeType.Scale);
            } else if (e == "rotate") {
                this.ctrl.switchMode(ModeType.Rotate);
            }
        });

        emitter.on("updateBoneData", (e) => {
            this.ctrl.updateBoneLength(e.length);
        });

        emitter.on("hoverItemEnter", (e) => {
            this.ctrl.hoverItemEnter(e);
        });

        emitter.on("hoverItemLeave", (e) => {
            this.ctrl.hoverItemLeave(e);
        });

        emitter.on("clickBone", (e) => {
            this.ctrl.clickBoneFromUI(e);
        });

        emitter.on("createDisplay", (e) => {
            this.ctrl.createDisplay(e.path, e.boneName, e.name);
        })
    }
}